package com.hanhai.material.wages.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.wages.entity.SysAttendanceForm;
import com.hanhai.material.wages.service.SysAttendanceFormService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.hanhai.material.wages.entity.table.SysAttendanceFormTableDef.SYS_ATTENDANCE_FORM;
import static com.mybatisflex.core.query.QueryMethods.distinct;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2023/12/15 15:59:36
 */
@RestController
@RequestMapping("/attendance_form")
@Log4j2
public class SysAttendanceFormController {

    @Autowired
    private SysAttendanceFormService sysAttendanceFormService;

    /**
     * 添加。
     *
     * @param sysAttendanceForm
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody SysAttendanceForm sysAttendanceForm) {
        return sysAttendanceFormService.save(sysAttendanceForm);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return sysAttendanceFormService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysAttendanceForm
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    public Result<?> update(@RequestBody SysAttendanceForm sysAttendanceForm) {
        log.error("AttendanceForm:{}", sysAttendanceForm);
        boolean result = sysAttendanceFormService.updateById(sysAttendanceForm);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SysAttendanceForm getInfo(@PathVariable Serializable id) {
        return sysAttendanceFormService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysAttendanceForm> page(Page<SysAttendanceForm> page) {
        return sysAttendanceFormService.page(page);
    }

    @PostMapping("/list")
    public Result<?> getAttendanceForm(@RequestBody SysAttendanceForm sysAttendanceForm) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID.eq(sysAttendanceForm.getDirectlyManagerUserId()));
        queryWrapper.where(SYS_ATTENDANCE_FORM.WORK_DATE.eq(sysAttendanceForm.getWorkDate()));
        queryWrapper.where(SYS_ATTENDANCE_FORM.IS_END.eq(true));
        List<SysAttendanceForm> attendanceFormList = this.sysAttendanceFormService.list(queryWrapper);
        return attendanceFormList.isEmpty() ? ResultTool.fail() : ResultTool.success(attendanceFormList);
    }

    @PostMapping("/dataList")
    public Result<?> getDataList(@RequestBody SysAttendanceForm sysAttendanceForm) {
        log.error("SysAttendanceForm:{}",sysAttendanceForm.toString());
        QueryWrapper sysAttendanceFormQueryWrapper = new QueryWrapper();
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.WORK_DATE.eq(fromDateToString("yyyy-MM-dd",sysAttendanceForm.getWorkDate())));
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.DEPT.eq(sysAttendanceForm.getDept()));
        sysAttendanceFormQueryWrapper.select(
                distinct(SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID),
                SYS_ATTENDANCE_FORM.WORK_DATE,
                SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER,
                SYS_ATTENDANCE_FORM.DEPT,
                SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID);
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.IS_END.eq(true));
        List<SysAttendanceForm> attendanceFormList = this.sysAttendanceFormService.list(sysAttendanceFormQueryWrapper);
        log.error("SQL:{}",sysAttendanceFormQueryWrapper.toSQL());
        return attendanceFormList.isEmpty()?ResultTool.fail():ResultTool.success(attendanceFormList);
    }
    @PostMapping("/resultList")
    public Result<?> getResultList(@RequestBody SysAttendanceForm attendanceForm){
        log.error("SysAttendanceForm:{}",attendanceForm.toString());
        QueryWrapper sysAttendanceFormQueryWrapper = new QueryWrapper();
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.WORK_DATE.eq(fromDateToString("yyyy-MM-dd",attendanceForm.getWorkDate())));
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.DEPT.eq(attendanceForm.getDept()));
        sysAttendanceFormQueryWrapper.select(distinct(SYS_ATTENDANCE_FORM.SUMMARY));
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.IS_END.eq(true));
        sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID.eq(attendanceForm.getDirectlyManagerUserId()));
        List<SysAttendanceForm> attendanceFormList = this.sysAttendanceFormService.list(sysAttendanceFormQueryWrapper);
        log.error("SQL:{}",sysAttendanceFormQueryWrapper.toSQL());
        for(SysAttendanceForm sysAttendanceForm:attendanceFormList){
            sysAttendanceFormQueryWrapper = new QueryWrapper();
            sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.WORK_DATE.eq(fromDateToString("yyyy-MM-dd",attendanceForm.getWorkDate())));
            sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.DEPT.eq(attendanceForm.getDept()));
            sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.IS_END.eq(true));
            sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.SUMMARY.eq(sysAttendanceForm.getSummary()));
            sysAttendanceFormQueryWrapper.where(SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID.eq(attendanceForm.getDirectlyManagerUserId()));
            List<SysAttendanceForm> attendanceFormChildList = this.sysAttendanceFormService.list(sysAttendanceFormQueryWrapper);
            sysAttendanceForm.setAttendanceFormList(attendanceFormChildList);
        }
        return attendanceFormList.isEmpty()?ResultTool.fail():ResultTool.success(attendanceFormList);
    }

    @PostMapping("/endSign")
    public Result<?> endSign(@RequestBody SysAttendanceForm sysAttendanceForm){
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.where(SYS_ATTENDANCE_FORM.DIRECTLY_MANAGER_USER_ID.eq(sysAttendanceForm.getDirectlyManagerUserId()));
        queryWrapper.where(SYS_ATTENDANCE_FORM.WORK_DATE.eq(sysAttendanceForm.getWorkDate()));
        queryWrapper.where(SYS_ATTENDANCE_FORM.IS_END.eq(false));
        List<SysAttendanceForm> attendanceFormList = this.sysAttendanceFormService.list(queryWrapper);
        attendanceFormList.forEach(item->item.setEnd(true));
        boolean result=this.sysAttendanceFormService.updateBatch(attendanceFormList);
        return result?ResultTool.success():ResultTool.fail();
    }



}
